Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@koa/cors is a middleware for Koa.js that enables Cross-Origin Resource Sharing (CORS) with various configuration options. It allows you to specify which origins are permitted to access your resources, as well as other CORS-related settings.
Basic CORS Setup
This code sets up a basic Koa server with CORS enabled for all origins. It uses the @koa/cors middleware without any additional configuration.
const Koa = require('koa');
const cors = require('@koa/cors');
const app = new Koa();
app.use(cors());
app.listen(3000);
Restricting Origins
This code configures the @koa/cors middleware to only allow requests from 'https://example.com'.
const Koa = require('koa');
const cors = require('@koa/cors');
const app = new Koa();
app.use(cors({ origin: 'https://example.com' }));
app.listen(3000);
Configuring Additional CORS Options
This code demonstrates how to configure additional CORS options such as allowed methods, headers, exposed headers, max age, and credentials.
const Koa = require('koa');
const cors = require('@koa/cors');
const app = new Koa();
app.use(cors({
origin: '*',
allowMethods: ['GET', 'POST'],
allowHeaders: ['Content-Type', 'Authorization'],
exposeHeaders: ['Content-Length', 'Date'],
maxAge: 3600,
credentials: true
}));
app.listen(3000);
The 'cors' package is a popular middleware for enabling CORS in Express.js applications. It offers similar functionality to @koa/cors but is designed for use with Express.js instead of Koa.js.
The 'fastify-cors' package is a CORS plugin for Fastify, a fast and low-overhead web framework for Node.js. It provides similar CORS configuration options as @koa/cors but is tailored for Fastify applications.
The 'hapi-cors' package is a CORS plugin for Hapi.js, a rich framework for building applications and services. It offers similar CORS functionality to @koa/cors but is designed for use with Hapi.js.
Cross-Origin Resource Sharing(CORS) for koa
$ npm install @koa/cors --save
Enable cors with default options:
const Koa = require('koa');
const cors = require('@koa/cors');
const app = new Koa();
app.use(cors());
/**
* CORS middleware
*
* @param {Object} [options]
* - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header
* - {String|Array} allowMethods `Access-Control-Allow-Methods`, default is 'GET,HEAD,PUT,POST,DELETE,PATCH'
* - {String|Array} exposeHeaders `Access-Control-Expose-Headers`
* - {String|Array} allowHeaders `Access-Control-Allow-Headers`
* - {String|Number} maxAge `Access-Control-Max-Age` in seconds
* - {Boolean|Function(ctx)} credentials `Access-Control-Allow-Credentials`, default is false.
* - {Boolean} keepHeadersOnError Add set headers to `err.header` if an error is thrown
* - {Boolean} secureContext `Cross-Origin-Opener-Policy` & `Cross-Origin-Embedder-Policy` headers.', default is false
* - {Boolean} privateNetworkAccess handle `Access-Control-Request-Private-Network` request by return `Access-Control-Allow-Private-Network`, default to false
* @return {Function} cors middleware
* @api public
*/
fengmk2 | dead-horse | omsmith | jonathanong | AlphaWong | cma-skedulo |
---|---|---|---|---|---|
erikfried | j-waaang | ltomes | lfreneda | matthewmueller | PlasmaPower |
swain | TyrealHu | xg-wang | lishengzxc | mcohen75 |
This project follows the git-contributor spec, auto updated at Fri Aug 19 2022 23:48:53 GMT+0800
.
FAQs
Cross-Origin Resource Sharing(CORS) for koa
The npm package @koa/cors receives a total of 562,193 weekly downloads. As such, @koa/cors popularity was classified as popular.
We found that @koa/cors demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.